﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class SingleAccordionStyle2EffectForm : ItemFormTemplate
    {
        public SingleAccordionStyle2EffectForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件效果\\手风琴\\SingleAccordionStyle2EffectForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        /// <summary>
        /// 信息通知
        /// </summary>
        private Dictionary<string, string> infoDic = new Dictionary<string, string>() {
            {"Staff" ,"3"}
        };

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;


            List<SingleAccordionNode> nodeList = new List<SingleAccordionNode>();
            {
                nodeList.Add(new HML.SingleAccordionLabelNode() { Text = "Admin" });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Payments", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Payments, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Noticeboard", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Noticeboard, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Taskboard", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Taskboard, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Hostel", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Hostel, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Transport", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Transport, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Attendance", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Attendance, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Leave", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Leave, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Settings", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Settings, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionLabelNode() { Text = "University" });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Dashboard", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Dashboard, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Professors", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Professors, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Staff", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Staff, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Students", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Students, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Departments", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Departments, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Courses", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Courses, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Library", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Library, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Holiday", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Holiday, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionLabelNode() { Text = "EXTRA" });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Calender", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Calender, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Chat App", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_ChatApp, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Contact", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Contact, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "FileManager", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_FileManager, TextLRMargin = new LRMargin(0, 0) });
                nodeList.Add(new HML.SingleAccordionContentNode() { Text = "Gallery", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style2_Gallery, TextLRMargin = new LRMargin(0, 0) });

            }
            this.demo_control.Size = new Size((int)Math.Ceiling(225 * this.demo_control.ScaleDpi), (int)Math.Ceiling(580 * this.demo_control.ScaleDpi));
            this.demo_control.BackColor = System.Drawing.Color.Transparent;
            this.demo_control.Font = new System.Drawing.Font("Bahnschrift SemiBold", 9.5F);
            this.demo_control.LabelNodeStyle.TextLRMargin = new LRMargin(8, 0);
            this.demo_control.StyleAppearance.LabelNode.Enter.BackColor = System.Drawing.Color.Transparent;
            this.demo_control.StyleAppearance.LabelNode.Normal.BackColor = System.Drawing.Color.Transparent;
            this.demo_control.StyleAppearance.LabelNode.Normal.TextColor = Color.FromArgb(128, 255, 255, 255);
            this.demo_control.StyleAppearance.LabelNode.Enter.TextColor = Color.FromArgb(128, 255, 255, 255);
            this.demo_control.ContentNodeStyle.TextLRMargin = new LRMargin(30, 0);
            this.demo_control.StyleAppearance.ContentNode.Enter.BackColor = System.Drawing.Color.Empty;
            this.demo_control.StyleAppearance.ContentNode.Normal.BackColor = System.Drawing.Color.Empty;
            this.demo_control.StyleAppearance.ContentNode.Selected.BackColor = System.Drawing.Color.Empty;
            this.demo_control.ContentNodeStyle.LRAnimationDistance = 0;
            this.demo_control.ContentNodeStyle.MouseAnimationAllTimer = 150;
            this.demo_control.StyleAppearance.ContentNode.Normal.TextColor = Color.White;
            this.demo_control.StyleAppearance.ContentNode.Selected.TextColor = Color.White;
            this.demo_control.ScrollBar.AutoHide = true;
            this.demo_control.Nodes.Clear();
            this.demo_control.Nodes.AddRange(nodeList.ToArray());
            this.demo_control.BackgroundDrawBefore += this.Demo_control_BackgroundDrawBefore;
            this.demo_control.NodeDrawBefore += this.Demo_control_NodeDrawBefore;
            this.demo_control.NodeDrawAfter += this.Demo_control_NodeDrawAfter;
            this.demo_control.NodeClick += this.Demo_control_NodeClick;

        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Height = Math.Min(1000, (control.Height - 80));
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
        }

        private void Demo_control_BackgroundDrawBefore(object sender, SingleAccordionBackgroundDrawBeforeEventArgs e)
        {
            Graphics g = e.Graphics;

            //渐变背景
            LinearGradientBrush back_sb = new LinearGradientBrush(e.Bounds_C, Color.FromArgb(0, 181, 173), Color.FromArgb(23, 162, 184), LinearGradientMode.Vertical);
            g.FillRectangle(back_sb, e.Bounds_C);
            back_sb.Dispose();

            //取消控件默认绘制
            e.Cancel = true;
        }

        private void Demo_control_NodeDrawBefore(object sender, SingleAccordionNodeDrawBeforeEventArgs e)
        {
            if (e.Node is SingleAccordionContentNode)
            {
                SingleAccordion sa = (SingleAccordion)sender;
                Graphics g = e.Graphics;
                float start_x = e.TextBounds_C.X;
                float interval = (int)Math.Ceiling(1.5);

                //选中内容节点颜色
                if (((SingleAccordion)sender).SelectedNode == e.Node)
                {
                    e.BackBrush.Color = Color.FromArgb(28, 33, 33, 33);
                    e.TextBrush.Color = Color.FromArgb(255, 255, 255);
                }
                //非选中内容节点颜色
                else
                {
                    e.BackBrush.Color = Color.Empty;
                    if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseEnter)
                    {
                        e.TextBrush.Color = Color.FromArgb(255, 255, 255);
                    }
                    else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseEntering)
                    {
                        e.TextBrush.Color = Color.FromArgb(Math.Max(128, Math.Min(byte.MaxValue, 128 + (int)Math.Ceiling((255 - 128) * AnimationHelper.EaseOut(e.Node.MouseAnimationUsedTime, e.Node.GetMouseAnimationAllTime(), 0.5)))), 255, 255, 255);
                    }
                    else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseLeaveing)
                    {
                        e.TextBrush.Color = Color.FromArgb(Math.Max(128, Math.Min(byte.MaxValue, 128 + (int)Math.Ceiling((255 - 128) * (1 - AnimationHelper.EaseOut(e.Node.MouseAnimationUsedTime, e.Node.GetMouseAnimationAllTime(), 0.5))))), 255, 255, 255);
                    }
                    else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseLeave)
                    {
                        e.TextBrush.Color = Color.FromArgb(128, 255, 255, 255);
                    }
                }

                // 背景绘制
                g.FillRectangle(e.BackBrush, e.Bounds_C);
                // 图片绘制
                g.DrawImage(e.Image, e.ImageBounds_C);
                // 文本绘制
                if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseEnter)
                {
                    for (int i = 0; i < e.Node.Text.Length; i++)
                    {
                        Size shar_size = Size.Ceiling(g.MeasureString(e.Node.Text[i].ToString(), sa.Font, int.MaxValue, StringFormat.GenericTypographic));
                        g.DrawString(e.Node.Text[i].ToString(), sa.Font, e.TextBrush, new PointF(start_x, e.TextBounds_C.Y), StringFormat.GenericTypographic);
                        start_x += shar_size.Width + interval;
                    }
                }
                else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseEntering)
                {
                    for (int i = 0; i < e.Node.Text.Length; i++)
                    {
                        Size shar_size = Size.Ceiling(g.MeasureString(e.Node.Text[i].ToString(), sa.Font, int.MaxValue, StringFormat.GenericTypographic));
                        g.DrawString(e.Node.Text[i].ToString(), sa.Font, e.TextBrush, new PointF(start_x, e.TextBounds_C.Y), StringFormat.GenericTypographic);
                        start_x += shar_size.Width + Math.Max(0, (float)(interval * AnimationHelper.EaseOut(e.Node.MouseAnimationUsedTime, e.Node.GetMouseAnimationAllTime(), 0.5)));
                    }
                }
                else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseLeaveing)
                {
                    for (int i = 0; i < e.Node.Text.Length; i++)
                    {
                        Size shar_size = Size.Ceiling(g.MeasureString(e.Node.Text[i].ToString(), sa.Font, int.MaxValue, StringFormat.GenericTypographic));
                        g.DrawString(e.Node.Text[i].ToString(), sa.Font, e.TextBrush, new PointF(start_x, e.TextBounds_C.Y), StringFormat.GenericTypographic);
                        start_x += shar_size.Width + Math.Max(0, (float)(interval * (1 - AnimationHelper.EaseOut(e.Node.MouseAnimationUsedTime, e.Node.GetMouseAnimationAllTime(), 0.5))));
                    }
                }
                else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseLeave)
                {
                    for (int i = 0; i < e.Node.Text.Length; i++)
                    {
                        Size shar_size = Size.Ceiling(g.MeasureString(e.Node.Text[i].ToString(), sa.Font, int.MaxValue, StringFormat.GenericTypographic));
                        g.DrawString(e.Node.Text[i].ToString(), sa.Font, e.TextBrush, new PointF(start_x, e.TextBounds_C.Y), StringFormat.GenericTypographic);
                        start_x += shar_size.Width;
                    }
                }

                //取消控件默认绘制
                e.Cancel = true;
            }
        }

        private void Demo_control_NodeDrawAfter(object sender, SingleAccordionNodeDrawAfterEventArgs e)
        {
            //为 Staff 节点添加 信息通知
            for (int i = 0; i < this.infoDic.Count; i++)
            {
                if (this.infoDic.Keys.Contains(e.Node.Text))
                {
                    SingleAccordion sa = (SingleAccordion)sender;
                    Graphics g = e.Graphics;

                    SolidBrush brush = new SolidBrush(Color.Tomato);
                    NativeMethods.TEXTMETRIC font_metrics = ControlHelper.GetFontMetrics(this.Handle, sa.Font);
                    Size text_size = new Size((int)Math.Ceiling(g.MeasureString(this.infoDic[e.Node.Text], sa.Font, int.MaxValue, StringFormat.GenericTypographic).Width), font_metrics.GetFontRealHeight());
                    int rect_width = Math.Max(text_size.Width, text_size.Height) + 6;
                    RectangleF text_rect = new RectangleF(e.Bounds_C.Right - 16 - rect_width, e.Bounds_C.Y + (e.Bounds_C.Height - rect_width) / 2, rect_width, rect_width);
                    PointF text_point = new PointF(text_rect.X + (text_rect.Width - text_size.Width) / 2, text_rect.Y + (text_rect.Height - text_size.Height) / 2);

                    SmoothingMode sm = g.SmoothingMode;
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.FillEllipse(brush, text_rect);
                    g.SmoothingMode = sm;

                    brush.Color = Color.White;
                    g.DrawString(this.infoDic[e.Node.Text], sa.Font, brush, text_point, StringFormat.GenericTypographic);
                    brush.Dispose();

                    break;
                }
            }
        }

        private void Demo_control_NodeClick(object sender, SingleAccordionNodeEventArgs e)
        {
            //单击节点清除信息通知
            string key = "";
            for (int i = 0; i < this.infoDic.Count; i++)
            {
                if (this.infoDic.Keys.Contains(e.Node.Text))
                {
                    key = e.Node.Text;
                    break;
                }
            }
            if (key != "")
            {
                this.infoDic.Remove(key);
            }
            ((SingleAccordion)sender).Invalidate();
        }

        #endregion

    }
}
